package connexion;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;

import parameters.StaticParameters;



public class MASConnexion {
	Socket connection = null;
	ObjectOutputStream out;
	
	public static int stamp=0;
	IncomingMessageReader imr;
	int nb;
	
	String team;
	
	public MASConnexion(Socket socket) throws IOException {
		//1. creating a server socket
		ServerSocket providerSocket = new ServerSocket(2000+stamp);
		nb=stamp;
		stamp++;
		connection = socket;
		if(StaticParameters.SERVER_CONNEXION_MESSAGES_VERBOSITY)
			System.out.println("Connection received from " + connection.getInetAddress().getHostName());
		out = new ObjectOutputStream(connection.getOutputStream());
		out.flush();
		imr=new IncomingMessageReader(new ObjectInputStream(connection.getInputStream()));
	}
	
	public void sendMessage(Message msg)
	{
		try{
			out.writeObject(msg);
			out.flush();
			if(StaticParameters.SERVER_OUTGOING_MESSAGE_VERBOSITY)
				System.out.println("server:"+(nb)+">"+ msg);
		}
		catch(IOException ioException){
			ioException.printStackTrace();
		}
	}

	public Queue<Message> getMessages() {
		return imr.getMessages();
	}

	public String getTeam() {
		return team;
	}
	public void setTeam(String team){
		this.team=team;
	}
}
